/*
 * Copyright (c) 2009 Project E7
 *
 * This file is part of ZebBowling
 *
 * ZebBowling is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ZebBowling is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ZebBowling. If not, see <http://www.gnu.org/licenses/>.
 **/

package org.zebbowling.world;

import java.awt.Color;
import java.awt.Graphics;

/**
 * This class is used to represent a default {@code CanvasObject}.
 *
 * @author Mark Vismonte
 */
public class CanvasObject extends Object
    {
    //Data members
    private int xPos;
    private int yPos;
    private int width;
    private int height;
    private int xVel;
    private int yVel;
    private Color color;

    /**
     * Creates a CanvasObject with default settings
     */
    public CanvasObject()
        {
        this(120,600, 30, 30, 0,0, Color.BLACK);
        }

    /**
     * Creates a {@code CanvasObject} with specified settings
     *
     * @param x The x coordinate of this {@code CanvasObject}.
     * @param y The y coordinate of this {@code CanvasObject}.
     * @param width The width of this {@code CanvasObject}.
     * @param height The height of this {@code CanvasObject}.
     * @param xVel The x velocity of this {@code CanvasObject}.
     * @param yVel The y velocity of this {@code CanvasObject}.
     * @param color The {@linkplain Color} of this {@code CanvasObject}.
     */
    public CanvasObject(int x, int y, int width, int height, int xVel, int yVel
            , Color color)
        {
        super();
        setXPos(x);
        setYPos(y);
        this.width = width;
        this.height = height;
        setXVel(xVel);
        setYVel(yVel);
        setColor(color);
        }

    /**
     * Determines if this and the other collide.
     *
     * @param other An other object
     * @return true if they collide, false if not
     */
    public boolean collisionWith(CanvasObject other)
        {
        boolean isX = ((xPos + width) >= other.getXPos() && other.getXPos() >=
                getXPos()) || ((other.getXPos() + other.getWidth()) >= getXPos()
                && getXPos() >= other.getXPos());
        boolean isY = ((yPos + height) >= other.getYPos() && other.getYPos() >=
                getYPos()) || ((other.getYPos() + other.getHeight()) >= getYPos()
                && getYPos() >= other.getYPos());
        return isX && isY;
        }

    /**
     * Updates the location depending on the velocity
     */
    public void update()
        {
        setPosition(getXVel() + getXPos(), getYVel() + getYPos());
        }

    /**
     * Returns the x coordinate of this {@code CanvasObject}.
     *
     * @return The x coordinate of this {@code CanvasObject}.
     */
    public int getXPos() {
        return xPos;
        }

    /**
     * Sets the x coordinate of this {@code CanvasObject}.
     *
     * @param xPos The new x coordinate of this {@code CanvasObject}.
     */
    public void setXPos(int xPos) {
        if (xPos > 0 && xPos < BowlingFrame.GAME_WIDTH)
            this.xPos = xPos;
        }

    /**
     * Returns the x velocity of this {@code CanvasObject}.
     *
     * @return The x velocity of this {@code CanvasObject}
     */
    public int getXVel() {
        return xVel;
        }

    /**
     * Sets the x velocity of this {@code CanvasObject}.
     *
     * @param xVel The new x velocity of this {@code CanvasObject}.
     */
    public void setXVel(int xVel) {
        this.xVel = xVel;
        }

    /**
     * Sets the x and y coordinates of this {@code CanvasObject}.
     *
     * @param x The new x coordinate of this {@code CanvasObject}.
     * @param y The new y coordinate of this {@code CanvasObject}.
     */
    public void setPosition(int x, int y)
        {
        setXPos(x);
        setYPos(y);
        }

    /**
     * Sets the x and y velocities of this {@code CanvasObject}.
     *
     * @param x The new x velocity of this {@code CanvasObject}.
     * @param y The new y velocity of this {@code CanvasObject}.
     */
    public void setVel(int x, int y)
        {
        setYVel(y);
        setXVel(x);
        }

    /**
     * Sets the x and y velocities of this {@code CanvasObject}.
     *
     * @param vel The new x and y velocity of this {@code CanvasObject}.
     */
    public void setVel(int vel)
        {
        setYVel(vel);
        setXVel(vel);
        }

    /**
     * Returns the y coordinate of this {@code CanvasObject}.
     *
     * @return The y coordinate of this {@code CanvasObject}
     */
    public int getYPos() {
        return yPos;
        }

    /**
     * Sets the y coordinate of this {@code CanvasObject}.
     *
     * @param yPos The new y coordinate of this {@code CanvasObject}.
     */
    public void setYPos(int yPos) {
        if (yPos > -50 && yPos < BowlingFrame.GAME_HEIGHT)
            this.yPos = yPos;
        if (yPos <= 0)
            {
            setVel(0);
            }
        }

    /**
     * Returns the y velocity of this {@code CanvasObject}.
     *
     * @return The y velocity of this {@code CanvasObject}
     */
    public int getYVel() {
        return yVel;
        }

    /**
     * Sets the x coordinate of this {@code CanvasObject}.
     *
     * @param yVel The new x coordinate of this {@code CanvasObject}.
     */
    public void setYVel(int yVel) {
        this.yVel = yVel;
        }

    /**
     * Returns the height of this {@code CanvasObject}.
     *
     * @return The height of this {@code CanvasObject}.
     */
    public int getHeight() {
        return height;
        }

    /**
     * Returns the width of this {@code CanvasObject}.
     *
     * @return The width of this {@code CanvasObject}.
     */
    public int getWidth() {
        return width;
        }

    /**
     * Returns the {@code Color} of this {@code CanvasObject}.
     *
     * @return The {@code Color} of this {@code CanvasObject}.
     */
    public Color getColor() {
        return color;
        }

    /**
     * Sets the {@code Color} of this {@code CanvasObject}.
     *
     * @param color The new {@code Color} of this {@code CanvasObject}.
     */
    public void setColor(Color color) {
        this.color = color;
        }

    /**
     * Draws this {@code CanvasObject} to graphics.
     *
     * @param drawPad The graphics to draw this {@code CanvasObject} to.
     */
    public void draw(Graphics drawPad)
        {
        Color temp = drawPad.getColor();
        drawPad.setColor(this.getColor());
        drawPad.fillRect(xPos, yPos, width, height);
        drawPad.setColor(temp);
        }

    }
